<!--
 * @Author: licongwei
 * @Date: 2022-02-10 15:29:56
 * @Description: 图表容器
-->
<template>
  <div class="chart-box">
    <div
      class="charts"
      ref="chartEl"
      :style="{
        width: params.width ? params.width : '100%',
        height: params.height ? params.height : '100%'
      }"
    ></div>
    <loading v-show="params.loading" />
  </div>
</template>
<script lang="ts">
import { defineComponent, onBeforeUnmount, ref, watch } from 'vue'
import loading from './loading.vue'
import CreateChart from './CreateChart'
import { useStore } from 'vuex'
export default defineComponent({
  name: 'charts',
  components: {
    loading
  },
  props: {
    params: {
      type: Object,
      default() {
        return {
          loading: true,
          map: {},
          options: {},
          event: {},
          custom: false,
          carousel: {}
        }
      }
    }
  },
  setup(props: any) {
    const chartEl = ref<HTMLElement | null>(null)
    const store = useStore()
    let chart: CreateChart
    onBeforeUnmount(() => {
      chart && chart.dispose()
      chart && chart.clearInterval()
    })
    watch(
      () => props.params.loading,
      (newVal) => {
        if (!newVal) {
          chart && chart.clearInterval()
          // 待完善：考虑简化传参
          chart = new CreateChart(
            chartEl.value,
            props.params.map,
            props.params.forceRedraw,
            props.params.adcode,
            props.params.options,
            props.params.event,
            props.params.custom,
            props.params.carousel
          )
        }
      }
    )
    watch(
      () => store.state.scroll,
      (newVal) => {
        if (chart?.isScroll || false) {
          chart.isScroll(newVal)
        }
      }
    )
    return {
      chartEl
    }
  }
})
</script>
<style lang="less" scoped>
.chart-box {
  position: relative;
  width: 100%;
  height: 100%;
  .charts {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
    font-weight: bold;
  }
}
</style>
